package com.lg.system.wxgroup.service.impl;

import com.lg.system.wxgroup.entity.WxGroup;
import com.lg.system.menu.entity.Menu;
import com.lg.system.wxgroup.dao.WxGroupDao;
import com.lg.system.wxgroup.service.WxGroupService;
import com.lg.utils.BuildTree;
import com.lg.utils.Tree;

import me.chanjar.weixin.mp.bean.tag.WxUserTag;

import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import java.util.List;
import java.util.Map;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;

import org.springframework.transaction.annotation.Transactional;
import org.apache.commons.beanutils.BeanUtils;
import org.springframework.stereotype.Service;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author dl
 * @since 2018-04-08
 */
@Transactional
@Service
public class WxGroupServiceImpl extends ServiceImpl<WxGroupDao, WxGroup> implements WxGroupService {

	
	public boolean save( WxGroup wxGroup) {
		return super.insert(wxGroup);
	}

	@Override
	public boolean update(WxGroup wxGroup) {
		return super.updateById(wxGroup);
	}
	
	
	@Override
	public boolean deleteById(Long id) {
		return super.deleteById(id);
	}

	@Override
	public boolean deleteByIds(List<Long> ids) {
		if(null==ids||ids.isEmpty()){
			return false;
		}
		return super.deleteBatchIds(ids);
	}

	@Override
	public boolean initGroup(List<WxUserTag> wxUserTags) {
		if(!wxUserTags.isEmpty()){
			for(WxUserTag wxUserTag :wxUserTags){
				WxGroup wxGroup=new WxGroup();
				try {
					BeanUtils.copyProperties(wxGroup, wxUserTag);
				} catch (IllegalAccessException | InvocationTargetException e) {
					e.printStackTrace();
				}
				insert(wxGroup);
			}
		}
		return true;
	}

	@Override
	public Tree<WxGroup> selectTreeList() {
		Map<String, Object> map=new HashMap<>(16);
		List<WxGroup> group=selectByMap(map);
		if(group!=null&&!group.isEmpty()){
			List<Tree<WxGroup>> trees = new ArrayList<Tree<WxGroup>>();
			for (WxGroup g : group) {
				Tree<WxGroup> tree = new Tree<WxGroup>();
				tree.setId(g.getId()+"");
				tree.setName(g.getName());
				tree.setSpread(true);
				trees.add(tree);
			}
			Tree<WxGroup> t = BuildTree.build(trees);
			return t;
		}else{
			return null;
		}
	}

}
